var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {},
    role: false,
    token: null,
    type: '3',
    tx: false,
    txInfo: {}
  },
  onLoad: function (options) {
    var that = this;
  },
  onReady: function () {

  },
  //下拉刷新 
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载 //模拟加载 
    var that = this;
    setTimeout(function (e) {
      //刷新数据
      that.onShow();
      // complete 
      wx.hideNavigationBarLoading()
      //完成停止加载 
      wx.stopPullDownRefresh()
      //停止下拉刷新 
    });
  },
  onShow: function () {

    let token = wx.getStorageSync('token');
    let that = this;
    // 页面显示
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          userInfo: res.userInfo,
          token: token
        })
      }
    })
    //判断是否登录
    if (token == '' || !token) {
      token = null;
    } else {
      //已经登录，获取用户信息
      var customerId = wx.getStorageSync("customerId");
      util.request(api.UserDetail, { id: customerId }).then(function (res) {
        if (res.code === '200') {
          that.setData({
            customerInfo: res.datas,
            type: res.datas.type
          })
          wx.setStorageSync("type", res.datas.type);
          wx.setStorageSync("token",res.datas.token);
          wx.setStorageSync("auth", res.datas.auth);
          if (res.datas.type == '1' || res.datas.type == '0') {
            wx.setStorageSync("shareId", res.datas.id);
          }
          if (res.datas.recommendId) {
            wx.setStorageSync("shareId", res.datas.recommendId);
          }
          wx.setStorageSync("auth", res.datas.auth ? res.datas.auth : "0")
        }
      });
    }

  },
  getData: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  exitLogin: function () {
    var token = wx.getStorageSync("token");
    if (token) {
      wx.showModal({
        title: '',
        confirmColor: '#b4282d',
        content: '退出登录？',
        success: function (res) {
          if (res.confirm) {
            wx.removeStorageSync('token');
            wx.removeStorageSync('openId');
            wx.removeStorageSync('userInfo');
            wx.removeStorageSync("type");
            wx.removeStorageSync("customerId");
            wx.switchTab({
              url: '/pages/index/index-new'
            });
          }
        }
      })
    }

  },
  bindOrder: function () {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/order/order',
    })
  },
  bindCollect: function () {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/collect/collect',
    })
  },
  bindCoupon: function () {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/coupon/mycoupon',
    })
  },
  bindContact: function () {
    wx.makePhoneCall({
      phoneNumber: '4000282666',
    })
  },
  bindAboutUs: function () {
    wx.navigateTo({
      url: '/pages/ucenter/aboutus/index',
    })
  },
  fnChange: function (e) {
    let that = this;
    var role = !that.data.role;
    wx.setStorageSync("role", role);
    this.setData({
      role: role
    })
  },
  fnLogin: function (e) {
    wx.navigateTo({
      url: '/pages/auth/register/register',
    })
  },
  fnShare: function () {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
   // var path = "/pages/index/index-new";
    //type=0，表示为供应商邀请分销商
   // app.fnShareGoods(path, 1, "../../static/images/toShare.png", "风光鱼旅游分销平台诚邀您加入", "0");

    wx.navigateTo({
      url: '/pages/ucenter/invite/index?shareType=0',
    })
  },
  fnSetting: function () {
    var that = this;
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != '0') {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/ucenter/auth/index',
            })
          }
        }
      })
      return;
    }
    this.setData({
      isSetting: true
    })
  },
  fnClose: function (e) {
    this.setData({
      isSetting: false
    })
  },
  fnCloseTx: function (e) {
    this.setData({
      tx: false
    })
  },
  fnSureTx: function (e) {

    var that = this;
    var txInfo = that.data.txInfo;
    if (txInfo == null || txInfo.account == null || txInfo.account == "" || txInfo.accountName == null || txInfo.accountName == "" || that.data.price == '' || that.data.price == null || txInfo.remark == '' || txInfo.remark == null) {
      var msg = '请完善提现信息！'
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    if (!util.checkBankNo(txInfo.account)) {
      msg = "银行账户无效!"
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    wx.setStorageSync("txInfo", txInfo);
    if (parseFloat(this.data.customerInfo.divided) - parseFloat(this.data.price) < 0) {
      return;
    }
    var data = {
      price: this.data.price,
      account: this.data.txInfo.account,
      accountName: this.data.txInfo.accountName,
      remark: this.data.txInfo.remark,
      token: wx.getStorageSync("token")
    }
    wx.showLoading({
      title: '加载中。。。',
      mask: true
    })
    util.request(api.Tx, data).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        //关闭提现页面
        var customerInfo = that.data.customerInfo;
        customerInfo.divided = res.datas;
        that.setData({
          tx: false,
          customerInfo: customerInfo
        });

      }
    });
  },
  fnInputTxInfo: function (e) {
    var id = e.currentTarget.id;
    var txInfo = this.data.txInfo;
    txInfo['' + id] = e.detail.value;
    this.setData({
      txInfo: txInfo,
    });
    if ("divided" == id) {
      var price = e.detail.value;
      if (parseFloat(this.data.customerInfo.divided) - parseFloat(e.detail.value) < 0 || price < 0) {
        price = this.data.customerInfo.divided;
      }
      this.setData({
        price: price
      })
    }
  },
  fnInputNumber: function (e) {
    var value = e.detail.value;
    var customerInfo = this.data.customerInfo;
    customerInfo.dividedNumber = value;
    this.setData({
      customerInfo: customerInfo
    })
  },
  fnSure: function (e) {
    var that = this;
    var token = wx.getStorageSync("token");
    var data = {
      token: token,
      shareNumber: that.data.customerInfo.dividedNumber
    };
    if (that.data.customerInfo.dividedNumber > 0 && that.data.customerInfo.dividedNumber < 1) {
      util.request(api.UserSet, data).then(function (res) {
        if (res.code === '200') {
          //设置成功
          that.setData({
            isSetting: false
          })
          wx.showToast({
            title: '设置成功！',
          });
        } else {
          wx.showToast({
            title: '操作失败',
            duration: 1500,
            image: "/static/images/icon_error.png"
          })
        }
      });
    } else {
      wx.showToast({
        title: '数值不合法!',
        duration: 1500,
        image: "/static/images/icon_error.png"
      })
    }

  },
  fnTx: function (e) {
    var that = this;
    var shareMoney = that.data.customerInfo.divided;
    if (!shareMoney || shareMoney == 0) {
      wx.showModal({
        title: '无可用提现额度',
        content: '',
      })
      return;
    }
    var txInfo = wx.getStorageSync("txInfo");
    that.setData({
      tx: true,
      txInfo: txInfo == '' ? {} : txInfo,
      price: shareMoney
    })
  },
  fnEmpty: function () { },
  bindUpload: function (e) {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (this.data.customerInfo.type == '0') {
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/auth/index',
    })
  },
  fnShowDetail: function () {
    var token = wx.getStorageSync("token");
    var that = this;
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != '0') {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/ucenter/auth/index',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/dividedlist/index',
    })
  },
  fnToOrderList: function (e) {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  //进入邀请推广页面
  fnToShare: function (e) {
    var that = this;
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != 0) {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/ucenter/auth/index',
            })
          }
        }
      })
      return;
    }
    var url = "/pages/ucenter/shareIndex/index";
    wx.navigateTo({
      url: url,
    })
  }
})